import 'package:flutter/material.dart';
import '../components/message-row.dart';
import '../request/api.dart';

Api api =  new Api();

class MessagePage extends StatefulWidget {
  MessagePage({Key key}) : super(key: key);

  @override
  _MessagePageState createState() => _MessagePageState();
}

List<Widget> messageListWidget(List dataList) {
  List<Widget> list = [];
  for(var item in dataList){
    print(item);
    list.add(
      MessageRow(nickName: item['nickName'], message: item['message'], time: item['time'])
    );
  }
  return list;
}

class _MessagePageState extends State<MessagePage> {
  List messageList = [];

  @override
  void initState() {
    super.initState();
    // 接口数据样例
    // {
    //   "data": {
    //     "code": 0,
    //     "list": [
    //       {
    //         "id": "410000198112048459",
    //         "nickName": "戴芳",
    //         "message": "了空反在油七每支王报前拉采面备。",
    //         "time": 1595902839784
    //       },
    //       {
    //         "id": "350000199602034920",
    //         "nickName": "杨涛",
    //         "message": "消发周地所完温热北统名除切王方科以。",
    //         "time": 1595902839784
    //       }
    //     ]
    //   }
    // }
    api.messageList()
    ..then((res) {
      if(res.data['data']['code'] == 0){
        setState(() {
          this.messageList = res.data['data']['list'];
        });
      }
    });   
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('消息'),
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.add_alert),
            tooltip: 'Show Snackbar',
            onPressed: () {
              
            },
          ),
          IconButton(
            icon: const Icon(Icons.navigate_next),
            tooltip: 'Next page',
            onPressed: () {
              
            },
          ),
        ],

      ),
      body: ListView(
        children: messageListWidget(messageList)
      ),
    );
  }
}
